<template>
  <div>
    <el-tree
      ref="tree"
      show-checkbox
      default-expand-all
      node-key="id"
      :data="list"
      :props="{label:'name'}"
    />
    <el-row type="flex" justify="end">
      <el-button type="primary" justify="end" @click="submit">确定</el-button>
      <el-button type="primary" justify="end">取消</el-button>
    </el-row>
  </div>
</template>
<script>
import { treeData } from '@/utils'
import { getPermissionList, getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async submit() {
      // this.$refs.tree.getCheckedKeys() 返回的是点击节点的所有key组成的数组
      const permIds = this.$refs.tree.getCheckedKeys()
      // console.log(permIds)
      await assignPerm({ id: this.curId, permIds: permIds })
      // console.log(res)
      this.$emit('close')
    },
    // 获取权限信息
    async loadPermissionList() {
      const res = await getPermissionList()
      const detail = await getRoleDetail(this.curId)
      // console.log(detail)
      // console.log(res)
      this.list = treeData(res.data)
      // setCheckedKeys()树形组件的方法 数据回填
      this.$refs.tree.setCheckedKeys(detail.data.permIds)
    }
  }
}
</script>
